package demo;

import api.GreetingService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.RpcContext;

/**
 * Copyright (c)  by www.leya920.com
 * All right reserved.
 * Create Date: 2020-11-21 16:16
 * Create Author: xuweiwei
 * File Name: ApiConsumer.java
 * Last version:  1.0
 * Function: //TODO
 * Last Update Date: 2020-11-21 16:16
 * Last Update Log:
 * Comment: 服务消费者
 **/
public class ApiConsumer {

    public static void main(String[] args) {

        // 9 创建服务引用对象实例
        ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<>();

        // 10 设置应用程序信息
        referenceConfig.setApplication(new ApplicationConfig("first-dubbo-consumer"));

        // 11 设置服务注册中心
        referenceConfig.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));

        // 12 设置服务接口  超时时间
        referenceConfig.setInterface(GreetingService.class);
        referenceConfig.setTimeout(5000);

        // 13 设置自定义负载均衡策略与集群容错策略
//        referenceConfig.setLoadbalance("myLoadBalance");
//        referenceConfig.setCluster("myBroadcast");

        // 14 设置服务分子与版本
        referenceConfig.setGroup("dubbo");
        referenceConfig.setVersion("1.0.0");

        // 15 引用服务
        GreetingService greetingService = referenceConfig.get();

        // 16 设置隐式参数
        RpcContext.getContext().setAttachment("company","alibaba i am learning");

        // 17 调用服务
        String response = greetingService.sayHello("world");
        System.out.println(response);


    }
}
